﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApAPPTranslationLimitsGlobalManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.AutomaticPathPlanner;
using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public class CApAPPTranslationLimitsGlobalManager
{
  private CApAPPLimitsViewModel m_limitsViewModel;

  public CApAPPTranslationLimitsGlobalManager()
  {
    this.TranslationLimitsWallDisplayer = new CApAPPTranslationLimitWallDisplayer();
  }

  private CApAPPTranslationLimitWallDisplayer TranslationLimitsWallDisplayer { get; set; }

  public List<CApAPPObjectFlowTask> ObjectFlowTaskList { get; set; }

  public CApAPPLimitsViewModel LimitsViewModel
  {
    get => this.m_limitsViewModel;
    set
    {
      this.m_limitsViewModel = value;
      this.TranslationLimitsWallDisplayer.LimitsViewModel = value;
    }
  }

  public void Restore()
  {
    if (this.ObjectFlowTaskList == null)
      return;
    foreach (CApAPPObjectFlowTask objectFlowTask in this.ObjectFlowTaskList)
      this.RestoreDefaultLimitsForTask(objectFlowTask);
  }

  private void RestoreDefaultLimitsForTask(CApAPPObjectFlowTask objectFlowTask)
  {
    (objectFlowTask.KiTask as KiAppObjectFlowTaskEx).ComputeDefaultTranslationLimits();
    KiAppLimitsEx limits = (objectFlowTask.KiTask as KiAppObjectFlowTaskEx).GetLimits();
    limits.TxDofStatus = (KiAppLimitDofStatus) 0;
    limits.TyDofStatus = (KiAppLimitDofStatus) 0;
    limits.TzDofStatus = (KiAppLimitDofStatus) 0;
  }

  public bool AreTranslationLimitsValidForTaskWithErrorMessage(
    CApAPPObjectFlowTask objectFlowTask,
    out string errorMessage,
    out List<CApAPPWaypoint> listOfWaypointsWithTranslationError)
  {
    errorMessage = "";
    List<CApAPPWaypoint> fixedWaypointList = objectFlowTask.ComputeFixedWaypointList();
    listOfWaypointsWithTranslationError = new List<CApAPPWaypoint>();
    List<CApAPPWaypoint> wpTranslationLimitsError = (List<CApAPPWaypoint>) null;
    double minRange = 0.0;
    double maxRange = 0.0;
    bool flag1 = objectFlowTask.AreTranslationLimitsValid(AxisType.AXIS_TX, CApAPPObjectFlowTask.LimitsCheckMode.ConsiderDofStatus, fixedWaypointList[0], fixedWaypointList, out wpTranslationLimitsError);
    if (!flag1)
    {
      listOfWaypointsWithTranslationError = wpTranslationLimitsError;
      double currentMinValue = objectFlowTask.Limits.TxDofStatus == 2 ? 0.0 : objectFlowTask.Limits.TxMinValue;
      double currentMaxValue = objectFlowTask.Limits.TxDofStatus == 2 ? 0.0 : objectFlowTask.Limits.TxMaxValue;
      this.ComputeTranslationRangeForAxis(AxisType.AXIS_TX, fixedWaypointList[0], wpTranslationLimitsError, currentMinValue, currentMaxValue, out minRange, out maxRange);
      errorMessage = "X " + this.ComputeTranslationErrorMessage(minRange, maxRange);
    }
    bool flag2 = objectFlowTask.AreTranslationLimitsValid(AxisType.AXIS_TY, CApAPPObjectFlowTask.LimitsCheckMode.ConsiderDofStatus, fixedWaypointList[0], fixedWaypointList, out wpTranslationLimitsError);
    if (!flag2)
    {
      listOfWaypointsWithTranslationError.InsertRange(listOfWaypointsWithTranslationError.Count, (IEnumerable<CApAPPWaypoint>) wpTranslationLimitsError);
      double currentMinValue = objectFlowTask.Limits.TyDofStatus == 2 ? 0.0 : objectFlowTask.Limits.TyMinValue;
      double currentMaxValue = objectFlowTask.Limits.TyDofStatus == 2 ? 0.0 : objectFlowTask.Limits.TyMaxValue;
      this.ComputeTranslationRangeForAxis(AxisType.AXIS_TY, fixedWaypointList[0], wpTranslationLimitsError, currentMinValue, currentMaxValue, out minRange, out maxRange);
      if (errorMessage != "")
        errorMessage += ", ";
      errorMessage = $"{errorMessage}Y {this.ComputeTranslationErrorMessage(minRange, maxRange)}";
    }
    bool flag3 = objectFlowTask.AreTranslationLimitsValid(AxisType.AXIS_TZ, CApAPPObjectFlowTask.LimitsCheckMode.ConsiderDofStatus, fixedWaypointList[0], fixedWaypointList, out wpTranslationLimitsError);
    if (!flag3)
    {
      listOfWaypointsWithTranslationError.InsertRange(listOfWaypointsWithTranslationError.Count, (IEnumerable<CApAPPWaypoint>) wpTranslationLimitsError);
      double currentMinValue = objectFlowTask.Limits.TzDofStatus == 2 ? 0.0 : objectFlowTask.Limits.TzMinValue;
      double currentMaxValue = objectFlowTask.Limits.TzDofStatus == 2 ? 0.0 : objectFlowTask.Limits.TzMaxValue;
      this.ComputeTranslationRangeForAxis(AxisType.AXIS_TZ, fixedWaypointList[0], wpTranslationLimitsError, currentMinValue, currentMaxValue, out minRange, out maxRange);
      if (errorMessage != "")
        errorMessage += ", ";
      errorMessage = $"{errorMessage}Z {this.ComputeTranslationErrorMessage(minRange, maxRange)}";
    }
    return flag1 & flag2 & flag3;
  }

  public string ComputeTranslationErrorMessage(double minRange, double maxRange)
  {
    double num1 = Math.Round(minRange - 0.005, 2);
    double num2 = Math.Round(maxRange + 0.005, 2);
    string rangeDescription = CApAPPMainWindowStringTable.PathPlanningTranslationRangeDescription;
    double num3 = num1 / TxApplication.Options.Units.LinearMultiplier;
    string str1 = num3.ToString(CApCableUtility.NumericLinearFormat);
    num3 = num2 / TxApplication.Options.Units.LinearMultiplier;
    string str2 = num3.ToString(CApCableUtility.NumericLinearFormat);
    return string.Format(rangeDescription, (object) str1, (object) str2);
  }

  private void UpdateInitialTranslationLimitsForTask(CApAPPObjectFlowTask objectFlowTask)
  {
    double txMinValue = 0.0;
    double tyMinValue = 0.0;
    double tzMinValue = 0.0;
    double txMaxValue = 0.0;
    double tyMaxValue = 0.0;
    double tzMaxValue = 0.0;
    objectFlowTask.ComputeMinTranslationLimitsValues(out txMinValue, out txMaxValue, out tyMinValue, out tyMaxValue, out tzMinValue, out tzMaxValue);
    List<CApAPPWaypoint> listOfWaypointsWithTranslationErrors = (List<CApAPPWaypoint>) null;
    if (objectFlowTask.AreTranslationLimitsValid(CApAPPObjectFlowTask.LimitsCheckMode.DontConsiderDofStatus, out listOfWaypointsWithTranslationErrors))
      return;
    KiAppLimitsEx limits = objectFlowTask.Limits;
    limits.TxMinValue = txMinValue;
    limits.TxMaxValue = txMaxValue;
    limits.TyMinValue = tyMinValue;
    limits.TyMaxValue = tyMaxValue;
    limits.TzMinValue = tzMinValue;
    limits.TzMaxValue = tzMaxValue;
  }

  public void UpdateInitialTranslationLimits()
  {
    foreach (CApAPPObjectFlowTask objectFlowTask in this.ObjectFlowTaskList)
      this.UpdateInitialTranslationLimitsForTask(objectFlowTask);
  }

  public void ClearTranslationLimitsWalls()
  {
    this.TranslationLimitsWallDisplayer.DeactivateTranslationLimitsWalls();
  }

  private bool AreTranslationLimitParametersInitialized(CApAPPObjectFlowTask task)
  {
    KiAppLimitsEx limits = (task.KiTask as KiAppObjectFlowTaskEx).GetLimits();
    return !double.IsInfinity(limits.TxMinValue) && !double.IsInfinity(limits.TxMaxValue) && !double.IsInfinity(limits.TyMinValue) && !double.IsInfinity(limits.TyMaxValue) && !double.IsInfinity(limits.TzMinValue) && !double.IsInfinity(limits.TzMaxValue);
  }

  public void ComputeTranslationRangeForAxis(
    AxisType TranslationAxisType,
    CApAPPWaypoint startPoint,
    List<CApAPPWaypoint> wpOutSideLimits,
    double currentMinValue,
    double currentMaxValue,
    out double minRange,
    out double maxRange)
  {
    minRange = 0.0;
    maxRange = 0.0;
    double num = 0.0;
    TxTransformation absoluteLocation1 = startPoint.SimulatedApproachLocation.AbsoluteLocation;
    foreach (CApAPPWaypoint wpOutSideLimit in wpOutSideLimits)
    {
      TxTransformation absoluteLocation2 = wpOutSideLimit.SimulatedApproachLocation.AbsoluteLocation;
      TxVector translation = TxTransformation.op_Multiply(absoluteLocation1.Inverse, absoluteLocation2).Translation;
      switch (TranslationAxisType)
      {
        case AxisType.AXIS_TX:
          num = translation.X;
          break;
        case AxisType.AXIS_TY:
          num = translation.Y;
          break;
        case AxisType.AXIS_TZ:
          num = translation.Z;
          break;
      }
      if (num < currentMinValue && num < minRange)
        minRange = num;
      if (num > currentMaxValue && num > maxRange)
        maxRange = num;
    }
  }

  public bool IsWallDisplayActivated
  {
    set => this.TranslationLimitsWallDisplayer.IsWallDisplayActivated = value;
  }
}
